--笃律负荆
local dulvfujin = fk.CreateSkill {
    name = "mcdh_dulvfujin",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

dulvfujin:addEffect(fk.TargetSpecifying,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and data.card.trueName == "slash" and target ~= player then
            local room = player.room
            local tar = data:getAllTargets()
            for _, p in ipairs(tar) do
                if p:getHandcardNum() <= player:getHandcardNum() then
                    return true
                end
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local tos = data.from
        local fromCards = {}
        local toCards = {}
        if not player:isKongcheng() then
            local number = math.min(2, player:getHandcardNum())
            fromCards = room:askForCard(player, number, number, false, self.name, false, ".", "#mcdh_dulvfujin-show")
        end
        if not tos:isKongcheng() then
            local number = math.min(2, tos:getHandcardNum())
            toCards = room:askForCard(tos, number, number, false, self.name, false, ".", "#mcdh_dulvfujin-show")
        end
        if #fromCards > 0 then
            player:showCards(fromCards)
        end
        if #toCards > 0 then
            tos:showCards(toCards)
        end
        local cards = {}
        table.insertTableIfNeed(cards, fromCards)
        table.insertTableIfNeed(cards, toCards)
        local choice = lib.MaxsuitToString(cards, true)
        local result = room:askForChoice(player, choice, self.name, "")
        local cards1 = table.filter(cards,
                function(id) return table.contains(fromCards, id) and Fk:getCardById(id):getSuitString(true) == result end)
        local cards2 = table.filter(cards,
                function(id) return table.contains(toCards, id) and Fk:getCardById(id):getSuitString(true) == result end)
        local moveInfos = {}
        if #cards1 > 0 then
            table.insert(moveInfos, {
                from = player,
                ids = cards1,
                toArea = Card.DiscardPile,
                moveReason = fk.ReasonDiscard,
                proposer = player,
                skillName = self.name,
            })
        end
        if #cards2 > 0 then
            table.insert(moveInfos, {
                from = tos,
                ids = cards2,
                toArea = Card.DiscardPile,
                moveReason = fk.ReasonDiscard,
                proposer = player,
                skillName = self.name,
            })
        end
        room:moveCards(table.unpack(moveInfos))
        if #cards1 == 0 then
            room:loseHp(player, 1, self.name)
        elseif #cards1 == 1 then
            player:drawCards(1, self.name)
        elseif # cards1 == 2 then
            player:drawCards(1, self.name)
            for _, p in ipairs(data:getAllTargets()) do
                data:cancelTarget(p)
            end
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_dulvfujin"] = "笃律负荆",
    [":mcdh_dulvfujin"] = "每回合限一次，其他角色使用【杀】指定目标时，若有手牌数不大于你的目标，你可以与此牌使用者同时展示两张手牌（无则不展示，不足则全部展示），然后你弃置其中一种花色最多的牌。若你此次弃置的牌数：为0，你失去1点体力；不小于1，你摸一张牌；为2，此【杀】无效。",
    ["#mcdh_dulvfujin-show"] = "笃律负荆: 展示两张手牌",
}

return dulvfujin